package com.android.mmchat.manager;

import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBManager {

	static class historyCache {
		public static final String TableName = "historyCache";

		public static final String KEY_ROWId = "id";
		public static final String KEY_IdKey = "IdKey";// 名字
		public static final String KEY_content = "content";//
		public static final String KEY_date = "date";

		public static final String[] columns = new String[] { KEY_ROWId,
				KEY_IdKey, KEY_content, KEY_date };
		public static final String Create_table = "create table " + TableName
				+ "(id integer primary key autoincrement, " + KEY_IdKey
				+ " text not null , " + KEY_content + " text not null,"
				+ KEY_date + " long not null " + ");";
	}
	
	private static final String DATABASE_NAME = "etouch.db";
	private static final int DATABASE_VERSION = 1;
	private final Context context;
	private static DatabaseHelper DBHelper = null;
	private static SQLiteDatabase db = null;

	private static DBManager dbManager = null;

	private DBManager(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		public void onCreate(SQLiteDatabase db) {
			db.execSQL(historyCache.Create_table);
		}

		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + historyCache.TableName);
			onCreate(db);
		}
	}// DatabaseHelper

	/**
	 * 打开数据库
	 */
	public synchronized static DBManager open(Context ctx) throws SQLException {
		if (dbManager != null) {

		} else {
			dbManager = new DBManager(ctx);
			db = DBHelper.getWritableDatabase();
		}
		return dbManager;
	}

	/**
	 * 关闭数据库
	 */
	public void close() {
		if (db != null) {
			db.close();
		}
		if (DBHelper != null) {
			DBHelper.close();
		}
		if (dbManager != null) {
			dbManager = null;
		}
	}

	// //////////////////////////////////////////////////
	/**
	 * 向缓存表中插入一条数据
	 */
	public void insertToCache(String IdKey, String content, long date) {
		ContentValues cv = new ContentValues();
		cv.put(historyCache.KEY_IdKey, IdKey);
		cv.put(historyCache.KEY_content, content);
		cv.put(historyCache.KEY_date, date);
		db.insert(historyCache.TableName, null, cv);
	}

	/**
	 * 更新缓存表中的一条数据
	 */
	public void updateToCache(String IdKey, String content, long date) {
		ContentValues cv = new ContentValues();
		cv.put(historyCache.KEY_IdKey, IdKey);
		cv.put(historyCache.KEY_content, content);
		cv.put(historyCache.KEY_date, date);
		db.update(historyCache.TableName, cv, historyCache.KEY_IdKey
				+ " LIKE ? ", new String[] { IdKey });

	}

	/**
	 * 根据key获取一条缓存
	 */
	public synchronized Cursor getCache(String IdKey) {
		Cursor cur=null;
		cur=db.query(historyCache.TableName, historyCache.columns,historyCache.KEY_IdKey + " LIKE ? ", new String[] { IdKey },
				null, null, null);
		return cur;
	}

	/**
	 * 根据key删除一条缓存
	 */
	public void deleteCache(String IdKey) {
		db.delete(historyCache.TableName, historyCache.KEY_IdKey + " LIKE ? ",
				new String[] { IdKey });
	}
}
